package com.braveman.im.ui.activity.message;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.braveman.im.R;
import com.braveman.im.utils.PermissionUtils;
import com.braveman.im.utils.QrcodeUtils;
import com.braveman.im.utils.UtilsStyle;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.vondear.rxtools.RxBarTool;
import com.vondear.rxtools.view.dialog.RxDialogSureCancel;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 二维码
 */
public class QrcodeForMineActivity extends BaseActivity {

    private ImageView mIvActivityShopCollectionQrcode;
    private TextView mBtnActivityShopCollectionSaveImg;

    private LinearLayout mRlMemberPhoto;
    private String qrCodeType;
    private RxDialogSureCancel rxDialogSureCancel;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("二维码").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtils.finishActivity(QrcodeForMineActivity.this);
            }
        });
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        qrCodeType = callIntent.getStringExtra("qrCodeType");
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_qr_code_formine;
    }

    @Override
    protected void initChildView() {

        mIvActivityShopCollectionQrcode = (ImageView) findViewById(R.id.iv_activity_shop_collection_qrcode);
        mBtnActivityShopCollectionSaveImg = (TextView) findViewById(R.id.btn_activity_shop_collection_saveImg);
        mRlMemberPhoto = findViewById(R.id.rl_member_photo);
    }

    @Override
    protected void initChildData() {
        ViewGroup.LayoutParams layoutParams = mIvActivityShopCollectionQrcode.getLayoutParams();
        Bitmap bitmap = QrcodeUtils.encodeAsBitmap(qrCodeType, layoutParams.width, layoutParams.height);
        mIvActivityShopCollectionQrcode.setImageBitmap(bitmap);
    }

    @Override
    protected void setChildViewListener() {
        mBtnActivityShopCollectionSaveImg.setOnClickListener(this);
        mIvActivityShopCollectionQrcode.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //提示弹窗
                rxDialogSureCancel = new RxDialogSureCancel(QrcodeForMineActivity.this);
                rxDialogSureCancel.getContentView().setText("是否保存图片");
                rxDialogSureCancel.getSureView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        saveImg();
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.getCancelView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rxDialogSureCancel.cancel();
                    }
                });
                rxDialogSureCancel.show();
                return true;
            }
        });
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.btn_activity_shop_collection_saveImg:

                break;
            default:
                break;
        }
    }

    //保存图片到系统相册
    private void saveImg() {
        mRlMemberPhoto.buildDrawingCache();
        Bitmap bitmap = mRlMemberPhoto.getDrawingCache();
        //6.0动态申请权限
        PermissionUtils.checkPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE");
        //将Bitmap 转换成二进制，写入本地
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArray = stream.toByteArray();
        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/钱包++");
        if (!dir.isFile()) {
            dir.mkdir();
        }
        File file = new File(dir, "QR_CODE.png");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(byteArray, 0, byteArray.length);
            fos.flush();
            // 把文件插入到系统图库
            try {
                MediaStore.Images.Media.insertImage(getContentResolver(),
                        file.getAbsolutePath(), "QR_CODE.png", null);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            // 通知图库更新
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(file);
            intent.setData(uri);
            sendBroadcast(intent);
            Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
